<?php

/**
 * XHTML_ColGroup, xhtml_lib
 * 
 * This file contains the source code for the XHTML_ColGroup class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_Table_Common_Element.php";
include_once "XHTML__Col_Element.php";
include_once "XHTML_Col.php";

/**
 * The XHTML_ColGroup class is a handler for the colgroup xhtml-strict tag as a part 
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_ColGroup extends XHTML_Table_Common_Element 
	implements XHTML__Col_Element {
    
    // inner elements
        
    /**
     * Defines the number of columns the <colgroup> should span.
     * 
     * @var integer
     */
    protected $span = 1;
    
    /**
     * Defines the width of the column group.
     * Note: The width attribute can be overridden by settings in <col>!
     *
     * @var number
     */
    protected $width;
    
    /**
     * Inner array of XHTML_Col.
     * 
     * @var array
     */
    protected $cols;
    
    
    // Methods
    
    /**
     * Constructs a XHTML_ColGroup object.
     */
    function __construct()
    {
    	parent::__construct();
    	settype($this->cols, "array");
    }
    
    /**
     * Sets the number of columns the <colgroup> should span.
     * 
     * @param integer $span
     */
    function setSpan($span)
    {
    	$this->span = $span;
    }
    
    
    /**
     * Gets the number of columns the <colgroup> should span.
     * 
     * @return integer
     */
    function getSpan()
    {
    	return $this->span;
    }
    
    /**
     * Sets the width of the column group.
     * Note: The width attribute can be overridden by settings in <col>!
     *
     * @param number $width
     */
    function setWidth($width)
    {
    	$this->width = $width;
    }
    
    /**
     * Gets the width of the column group.
     * Note: The width attribute can be overridden by settings in <col>!
     *
     * @return number
     */
    function getWidth()
    {
    	return $this->witdh;
    }
    
    /**
     * Sets the XHTML_Col elements array.
     *
     * @param array $cols
     */
    function setCols($cols) {
        $this->cols = $cols;
    }
    
    /**
     * Gets the XHTML_Col elements array.
     *
     * @return array
     */
    function getCols()
    {
        return $this->cols;
    }
    
    /**
     * Adds a XHTML_Col element to the inner array of this object.
     * 
     * @param XHTML_Col $col
     */
    function addCol(XHTML_Col $col)
    {
        array_push($this->cols, $col);
    }
    
    /**
     * Removes a XHTML_Col element from the inner array of this object.
     * 
     * @param XHTML_Col $col
     */
    function removeCol(XHTML_Col $col)
	{
        if (($res = array_search($col, $this->cols)) !== false) {
            unset($this->cols[$res]);
        }
    }
 
    // Overriding the toString method for a proper implementation.
    
    /**
    * Returns the string representation for echoing the class atributes.
    *
    * @return string
    */
    function toString()
    {
    	$auxString = "<colgroup".parent::toString();
    	
    	if (isset($this->span)) {
    		$auxString = $auxString."span=\"".$this->span."\" ";
    	}
    	
    	if (isset($this->width)) {
    		$auxString = $auxString."witdh=\"".$this->width."\" ";
    	}
    	
    	$auxString = $auxString.">\n";
    	
        if (count($this->cols) > 0) {
            foreach ($this->cols as $colw) {
                $auxString = $auxString.$col->toString();
            }
        }
    	
    	$auxString = $auxString."</colgroup>\n";
                
        return $auxString;
    }
    
}